package com.example.hfiveandaplication.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;


import com.barradata.Application;

import java.lang.reflect.Field;

/**
 * Created by wangjing on 2017/12/6.
 */
public class ScreenUtils {

    /**
     * 记录系统状态栏的高度
     */
    private static int sStatusBarHeight;

    /**
     * getScreenWidth:得到屏幕宽度(像素点数). <br/>
     *
     * @return
     * @author wangheng
     */
    public static int getScreenWidth() {
        return getDisplayMetrics().widthPixels;
    }

    /**
     * getScreenHeight:得到屏幕高度(像素点数). <br/>
     *
     * @return
     * @author wangheng
     */
    public static int getScreenHeight() {
        return getDisplayMetrics().heightPixels;
    }

    /**
     * 获取屏幕分辨率：高*宽
     */
    public static String genScreenResolution() {
        return new StringBuilder().append(getScreenHeight()).append("*").append(getScreenWidth()).toString();
    }

    /**
     * getDensity:得到像素密度 <br/>
     *
     * @return
     * @author wangheng
     */
    public static float getDensity() {
        return getDisplayMetrics().density;
    }

    /**
     * getDensityDpi:得到每英寸的像素点数<br/>
     *
     * @return
     * @author wangheng
     */
    public static int getDensityDpi() {
        return getDisplayMetrics().densityDpi;
    }

    /**
     * getDisplayMetrics:返回DisplayMetrics对象，以方便得到屏幕相关信息. <br/>
     *
     * @return
     * @author wangheng
     */
    private static DisplayMetrics getDisplayMetrics() {
        DisplayMetrics dm = new DisplayMetrics();
        try {
            WindowManager manager = (WindowManager) Application.getContext().getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            if (display != null) {
                display.getMetrics(dm);
            } else {
                dm.setToDefaults();
            }
        } catch (Exception e) {
            DebugLog.e(String.valueOf(e.getMessage()));
        }
        return dm;
    }

    /**
     * 用于获取状态栏的高度。
     *
     * @return 返回状态栏高度的像素值。
     */
    public static int getStatusBarHeight(Context context) {
        if (sStatusBarHeight == 0) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = (Integer) field.get(o);
                sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sStatusBarHeight;
    }

}
